package wt.hpad.utils;

import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.binaryfork.spanny.Spanny;

/**
 * Created by wt on 17-8-4.
 */
public class TextUtil {

    /**
     * 获取 colors.xml 中的颜色
     *
     * @param context
     * @param color
     * @return
     */
    private static int getColor(Context context, int color) {
        return ContextCompat.getColor(context, color);
    }

    private static int getColor(int color) {
        return ContextCompat.getColor(AppUtil.getContext(), color);
    }

    /**
     * 给文本加上颜色, 颜色从 R.colors.*** 中获取
     *
     * @param context
     * @param text
     * @param color
     * @return
     */
    public static SpannableString colorText(Context context, String text, int color) {
        if (!TextUtils.isEmpty(text)) {
            SpannableString spannableString = Spanny.spanText(text, new ForegroundColorSpan(getColor(context, color)));
            return spannableString;
        } else {
            return null;
        }
    }


    public static Spanned fromHtmlText(String s) {
        Spanned result;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            result = Html.fromHtml(s, Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(s);
        }
        return result;
    }
}
